package cn.madis.ymcc.service.impl;

import cn.madis.ymcc.domain.CourseMarket;
import cn.madis.ymcc.domain.CourseUserLearn;
import cn.madis.ymcc.dto.PayNotifyDto;
import cn.madis.ymcc.mapper.CourseUserLearnMapper;
import cn.madis.ymcc.service.ICourseMarketService;
import cn.madis.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zt
 * @since 2022-08-07
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;
    @Override
    public CourseUserLearn checkUserLearn(Long loginId, Long courseId) {
        EntityWrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return selectOne(wrapper);

    }

    /**
     * 添加用户购买课程信息
     * @param aliPayNotifyDto
     */
    @Override
    public void addPayCourse(PayNotifyDto aliPayNotifyDto) {
        Map<String,Object> map = JSONObject.parseObject(aliPayNotifyDto.getExtParams(), Map.class);
        Long loginId =Long.valueOf(map.get("loginId").toString()) ;
        String courseId = map.get("courseId").toString();
        String[] split = courseId.split(",");

        Date now = new Date();
        for (String s : split) {
            CourseUserLearn courseUserLearn = selectLoginIdCourseId(Long.valueOf(s), loginId);
            CourseMarket courseMarket = courseMarketService.selectById(Long.valueOf(s));
            if (courseUserLearn == null){
                //为空添加CourseUserLearn
                CourseUserLearn courseUserLearnTmp = new CourseUserLearn();
                courseUserLearnTmp.setLoginId(loginId);
                courseUserLearnTmp.setStartTime(now);
                courseUserLearnTmp.setEndTime(DateUtils.addDays(now,courseMarket.getValidDays()));
                courseUserLearnTmp.setState(0);
                courseUserLearnTmp.setCourseId(Long.valueOf(s));
                courseUserLearnTmp.setCourseOrderNo(aliPayNotifyDto.getOrderNo());
                courseUserLearnTmp.setCreateTime(now);
                insert(courseUserLearnTmp);

            }else {
                //不为空修改CourseOrderNo，EndTime
                courseUserLearn.setCourseOrderNo(aliPayNotifyDto.getOrderNo());
                courseUserLearn.setEndTime(DateUtils.addDays(courseUserLearn.getEndTime(),courseMarket.getValidDays()));
                updateById(courseUserLearn);
            }

        }

    }


    public CourseUserLearn selectLoginIdCourseId(Long courseId, Long loginId) {
        EntityWrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("login_id",loginId);
        return selectOne(wrapper);
    }
}
